package com.ffapp.rainmonitorcustom.notice;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ffapp.rainmonitorcustom.R;
import com.ffapp.rainmonitorcustom.basis.BasisActivity;
import com.ffapp.rainmonitorcustom.basis.Constants;
import com.ffapp.rainmonitorcustom.bean.BaseListBeanOT;
import com.ffapp.rainmonitorcustom.bean.LoginBean;
import com.ffapp.rainmonitorcustom.bean.SystemNoticeListBean;
import com.ffapp.rainmonitorcustom.bean.SystemNoticeListData;
import com.ffapp.rainmonitorcustom.bean.WarnListBean;
import com.ffapp.rainmonitorcustom.bean.WarnListData;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.loopj.android.http.RequestParams;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.TimeUtils;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

public class SystemNoticeActivity extends BasisActivity {


    @BindView(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    MyAdapterList mAdapter;
    SystemNoticeListBean mListBean;


    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.system_notice_activity);
        setTitle("系统公告");
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setVerticalScrollBarEnabled(true);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mListBean.refresh();
                getList();
            }

            @Override
            public void onLoadMore() {
                getList();
            }
        });
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadingMoreEnabled(true);
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        mListBean = new SystemNoticeListBean();
        setListView();
        getList();
    }



    private void getList() {
        RequestParams params = new RequestParams();
        showProgress();
        params.put("userId", LoginBean.getInstance().getResultUser().getId());
        params.put(BaseListBeanOT.PAGE_NAME, mListBean.getNextPage());
        params.put(BaseListBeanOT.PAGE_SIZE_NAME, BaseListBeanOT.PAGE_SIZE);
        HttpRestClient.get(Constants.URL_SYS_ANN, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        SystemNoticeListBean listBean = (SystemNoticeListBean) result;
//                        WarnListBean bean = JSON.parseObject(listBean.getResultData(), WarnListBean.class);
                        mListBean.addListBean(listBean);
                        setListView();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                        setListView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        hideLoading();
                        onListViewComplete();
                    }
                },
                0, SystemNoticeListBean.class);

    }

    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    private void onListViewComplete() {
        mRecyclerView.refreshComplete();
        mRecyclerView.loadMoreComplete();
        mRecyclerView.setLoadingMoreEnabled(mListBean.hasNextPage());
//        mRecyclerView.setLoadingMoreEnabled(false);
        setEmptyView();
    }

    private void setEmptyView() {
        if (mListBean == null  ||   mListBean.getList().size() == 0) {
            findViewById(R.id.refresh_view).setVisibility(View.VISIBLE);
            findViewById(R.id.refresh_view).setOnClickListener(
                    new View.OnClickListener() {
                        public void onClick(View v) {
                            mRecyclerView.refresh();
                        }
                    });
        } else {
            findViewById(R.id.refresh_view).setVisibility(View.INVISIBLE);
        }
    }



    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {

        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.system_notice_list_item, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_title)
            public TextView text_title;
            @BindView(R.id.text_content)
            public TextView text_content;
            @BindView(R.id.text_man)
            public TextView text_man;
            @BindView(R.id.text_time)
            public TextView text_time;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                SystemNoticeListData data = mListBean.getList().get(position);
                text_title.setText(data.getTitle());
                text_content.setText(data.getContent());
                text_man.setText(data.getAdmin());
                text_time.setText(TimeUtils.getTimeLongToStrByFormat(data.getModitime(), "yyyy-MM-dd HH:mm:ss"));
                list_item.setTag(position);
            }

            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
//                int position = (int) v.getTag();
//                WarnListData data = mListBean.getList().get(position);

            }


        }
    }

}

